import { EnumManager_blxf } from "../Manager/EnumManager";
import AudioModule_blxf from "../Module/AudioModule";
import { NumModule_blxf } from "../Module/NumModule";
import PublicModule_blxf from "../Module/PublicModule";
import { UtilsModule_blxf } from "../Module/UtilsModule";




/** 新年阶段 */
enum NewYearTimeType {
    NEW_START       = '2024/2/9',
    NEW_END         = '2024/2/17'
}

const smallPriceMsg: string = `送给孩子们的温暖，感谢您的善举&您的爱心，填满了孩子们的餐桌&感谢您，给留守儿童带来美味和关怀&您的慷慨，温暖了孩子们的心&爱心菜品，温暖留守儿童的每一天&您的善行，给孩子们带来希望和温暖`;


const {ccclass, property} = cc._decorator;

/** 游戏旁白 */

@ccclass
export default class DiagueItem_blxf extends cc.Component {

    @property({ type: cc.Node, tooltip: '客人台词' })
    guestDialogueNode: cc.Node = null;

    @property({ type: cc.Node, tooltip: '台词角标' })
    dialogueIcon: cc.Node = null;


    @property({ tooltip: '传入参数' })
    _options: any = null;

    @property({ tooltip: '客人id' })
    _guestId: number = null;

    @property({ tooltip: '客人数据' })
    _guestData: any = null;

    @property({ tooltip: '商品数据' })
    _shopData: any = null;

    @property({ tooltip: '是否显示' })
    _isShow: boolean = false;

    @property({ tooltip: '日常台词倒计时' })
    _dialogueTimer: number = -1;

    @property({ tooltip: '日常台词间隔时间' })
    _normalTime: number = -1;

    @property({ tooltip: '隐藏倒计时' })
    _hideTimer: number = -1;

    _nodeY: number = 0;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {
        // this._hideTimer = 0.01;
        // this._normalTime = Math.random() * 2;
    }

    update ( dt: number ) {
        // 主玩法台词是否显示
        this.onNodeOpacity_blxf();

        if ( this._hideTimer > 0 ) {
            this._hideTimer -= dt;
            if ( this._hideTimer <= 0 ) {
                this.updateDialogueScale( false );
            }
        } else {
            if ( this._normalTime > 0 ) {
                this._normalTime -= dt;
                if ( this._normalTime <= 0 ) {
                    this.onNormalDialogue_blxf();
                }
            }
        }

        let _h: number = this.guestDialogueNode.parent.height;
        this.dialogueIcon.y = -66 + (119.1 - _h) / 2;
    }

    /** 主玩法台词是否显示 */
    onNodeOpacity_blxf () {
        let { isDecoration } = this._options;
        let _page1: boolean = PublicModule_blxf.gamePlayPageId == 1;
        let _page2: boolean = PublicModule_blxf.gamePlayPageId == 2 && isDecoration;
        let _isShow: boolean = ( _page1 || _page2 ) && !PublicModule_blxf.pageTween;
        this.node.opacity = _isShow ? 255 : 0;
    }

    initData_blxf ( options: any ) {
        this._options = options;

        this.updateIconScale_blxf();
        // this.dialogueIcon.scaleX = guestId == 1 ? -1 : 1;

        this._nodeY = this.node.y;
    }

    updateIconScale_blxf () {
        let { guestId } = this._options;

        let _scaleX: number = guestId == 1 ? -1 : 1;
        this.node.scaleX = _scaleX;
        this.guestDialogueNode.parent.scaleX = _scaleX;
        this.dialogueIcon.x = [ 270, 0, 0 ][ guestId - 1];
    }

    /** 隐藏台词 */
    onHideDialogue_blxf () {
        this._hideTimer = 0;
        this.updateDialogueScale( false, true );
    }

    /** 更新客人数据 */
    updateGuestData_blxf ( guestData: any, shopData: any ) {
        this._shopData = shopData;
        this._guestData = guestData;

        let { GuestId } = this._guestData || {};
        try {
            if ( !this || !this.node ) return;
            this.node.y = this._nodeY + ( GuestId == 6 ? -65 : 0 );
        } catch ( e ) {
            console.log('e.stack >>', e.stack);
        }
    }

    /**
     * 显隐动画
     * @param isShow 是否显示
     */
    updateDialogueScale ( isShow: boolean, isHide: boolean = false ) {
        if ( this._isShow == isShow ) return;

        this._isShow = isShow;

        if ( isHide ) {
            this.node.scaleY = 0;
            this.onClearDiagueGuestId_blxf();
            return ;
        }

        let _t: number = 0.3, _delayTime: number = !isShow ? 0 : 0.5, _scaleY: number = isShow ? 1 : 0;
        let _dialogueBox: cc.Node = this.node
        cc.tween( _dialogueBox )
        .delay( _delayTime )
        .to( _t, { scaleY: _scaleY }, cc.easeSineInOut() )
        .call(()=>{
            // if ( !this._isShow ) this._normalTime = 10;
            if ( !isShow ) this.onClearDiagueGuestId_blxf();
        })
        .start();
    }

    onClearDiagueGuestId_blxf () {
        PublicModule_blxf.isDialogueDemo = false;

        let { isDecoration, guestId } = this._options;
        if ( isDecoration ) return;
        if ( PublicModule_blxf.dialogueGuestId != guestId ) return;

        PublicModule_blxf.dialogueGuestId = null;
    }

    /** 日常台词 */
    onNormalDialogue_blxf () {
        let { DIALOGUE_NORMAL, DIALOGUE_NEW_YEAR } = EnumManager_blxf.guestDialogueType;
        let _isNewYear: boolean = this.isNewYear_blxf();
        this.loadGuestDialogue_blxf( !_isNewYear ? DIALOGUE_NORMAL : DIALOGUE_NEW_YEAR );
    }


    /**
     * 加载台词
     * @param dailogueType 台词类型 
     */
    loadGuestDialogue_blxf ( dialogueType: string = EnumManager_blxf.guestDialogueType.DIALOGUE_START, isAnimation: boolean = false  ) {
        // if ( !this._isShow ) return;
        if ( !this._guestData || !cc.isValid(this.node) ) return;

        let _dialogueStr: string = this._guestData[ dialogueType ];
        if ( !_dialogueStr ) {
            return UtilsModule_blxf.instance.Log_blxf(`未找到相关台词 talkType => ${dialogueType}`);
        }

        let _dialogueList: string[] = _dialogueStr.split('&');
        _dialogueList.sort((a, b) => Math.random() - 0.5);

        let _dialogueText: string = _dialogueList[ 0 ];

        let { ShopName } = this._shopData || { ShopName: '' };
        _dialogueText = _dialogueText.replace('【目标物品】', ShopName);

        this.updateDialogueNode_blxf( _dialogueText );
    }

    /**
     * 更新台词
     * @param msg 台词
     */
    updateDialogueNode_blxf ( msg: string, type: number = 1 ) {
        // let _isAlike: boolean = type == this._dialogueType;
        // !_isAlike && this.updateDialogueScale( false );

        if ( PublicModule_blxf.isDialogueDemo ) return;
        PublicModule_blxf.isDialogueDemo = true;

        let { guestId, isStopAudio, isDecoration } = this._options;
        let _isShow: boolean = this.node.opacity == 255;
        let _isAudio: boolean = PublicModule_blxf.gamePlayPageId == 1 && !isDecoration || PublicModule_blxf.gamePlayPageId == 2 && isDecoration;

        this._hideTimer = 2;
        this._normalTime = -1;
        this.updateDialogueScale( true );
        
        !isStopAudio && _isShow && _isAudio && AudioModule_blxf.instance.playAudio_blxf( EnumManager_blxf.gameAudioType.AUDIO_DIALOGUE );

        UtilsModule_blxf.instance.setText_blxf( this.guestDialogueNode, msg );
    }



    /** 检测是否处于新年阶段 */
    isNewYear_blxf () {
        let { NEW_START, NEW_END } = NewYearTimeType;
        let _startTime: number = new Date( NEW_START ).getTime();
        let _endTime: number = new Date( NEW_END ).getTime();
        let _nowTime: number = NumModule_blxf.instance.getTime_blxf();

        let _isGT: boolean = NumModule_blxf.instance.onCompareNum_blxf( _nowTime, _startTime );
        let _isLT: boolean = NumModule_blxf.instance.onCompareNum_blxf( _endTime, _nowTime );

        let _isNewYear: boolean = _isGT && _isLT;
        return _isNewYear;
    }

}
